#!/bin/bash
# 构建镜像和启动容器
docker build -t tantanshu:1.0 .

imagesId=`docker images | grep tantanshu | awk '{imagesIds=imagesIds " " $3} END{print imagesIds}'`

if [ -n "$imagesId" ]; then
  echo "构建完毕的镜像id为"$imagesId
  # 获取要构建的镜像名称和镜像版本号
  repositoryTag=`docker images | grep tantanshu | awk '{print $1 ":" $2}'`

  docker run -d -p 8085:8082 --name tantanshu-web01  $repositoryTag
  docker run -d -p 8086:8082 --name tantanshu-web02  $repositoryTag

  containerIds=`docker ps | grep tantanshu | awk '{containerIds=containerIds " " $1} END{print containerIds}'`
  if [ -n "$containerIds" ]; then
      echo "启动的容器id为"$containerIds
  fi
else
  echo "镜像id为空"
fi